#include <rclcpp/rclcpp.hpp>
#include <rclcpp/timer.hpp>
#include <std_msgs/msg/string.hpp>
#include <chrono>

using namespace std::chrono_literals;

class PrintNode:public rclcpp::Node{
public:
    PrintNode(std::string name):Node(name){
        this->declare_parameter("print_str","");
        pub_=this->create_publisher<std_msgs::msg::String>("print_string_pub",10);
        timer_=this->create_wall_timer(1s,std::bind(&PrintNode::timerCallback,this));
    }

    void timerCallback(){
        // 尝试获取参数并打印调试信息
        if (!this->has_parameter("print_str")) {
            RCLCPP_WARN(this->get_logger(), "Parameter 'print_str' was not found.");
            return;
        }

        std::string param_value;
        if (this->get_parameter("print_str", param_value)) {
            auto param_str = std_msgs::msg::String();
            param_str.data = param_value; // 将获取到的参数值赋给data字段
            pub_->publish(param_str);
        } else {
            RCLCPP_WARN(this->get_logger(), "Failed to get parameter 'print_str'");
        }
    }
      
private:
    rclcpp::Publisher<std_msgs::msg::String>::SharedPtr pub_{nullptr};
    rclcpp::TimerBase::SharedPtr timer_{nullptr};
};

int main(int argc,char** argv){
    rclcpp::init(argc,argv);
    auto node=std::make_shared<PrintNode>("print_node");
    rclcpp::spin(node);
    rclcpp::shutdown();

    return 0;
}